package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthMenu;
import com.elsa.DataStatistics.pojo.YearMenu;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class YearMenuStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private YearMenuMapper yearMenuMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void yearStatistics(String oId) {

        int yId = timeUtil.getYearTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            YearMenu yearMenu = yearMenuMapper.selectYearMenuFromDIdAndMId(yId, menuOrderList.get(i).getMId());
            if(yearMenu == null) {
                yearMenu = new YearMenu();
                yearMenu.setYmId(yearMenuMapper.selectCount(null) + 1);
                yearMenu.setYId(yId);
                yearMenu.setMId(menuOrderList.get(i).getMId());
                yearMenu.setMName(menuOrderList.get(i).getMName());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearMenu.setYmIncome(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                yearMenu.setYmExpand(menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = yearMenu.getYmIncome() - yearMenu.getYmExpand();
                yearMenu.setYmProfit(profit);
                yearMenu.setYmNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(yearMenu));
                yearMenuMapper.insert(yearMenu);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearMenu.setYId(yearMenu.getYId());
                yearMenu.setMName(menuOrderList.get(i).getMName());
                yearMenu.setYmIncome(yearMenu.getYmIncome() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                yearMenu.setYmExpand(yearMenu.getYmExpand() + menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = (menu.getMPrice() * menuOrderList.get(i).getMoNum()) - (menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                yearMenu.setYmProfit(yearMenu.getYmProfit() + profit);
                yearMenu.setYmNum(yearMenu.getYmNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(yearMenu));
                yearMenuMapper.updateById(yearMenu);
            }
        }
    }

}
